package com.mt.fpb.utils;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 解析xml 解析扫码返回过来的xml报文信息，（只解析了部分有用信息）
 * @author yu
 * @date 2021年08月24日 15:58
 */
public class ParseXml {

    /**
     *解析一码支付返回报文的方法
     * @Author yu
     * @date 2021/8/24 18:27
     * @Param [xml]
     * @return java.util.Map
     **/

    public static Map<String,String> parseXml(String  xml){
        Map<String,String> map = new HashMap<String,String>();
        //创建SAXReader对象
        SAXReader reader =  new SAXReader();
        //通过该对象读取输入的内容，使用文档对象接收’
        try {
            //Document document = reader.read(in);
            Document document = DocumentHelper.parseText(xml);
            //获取文档里的的根节点
            Element root =document.getRootElement();
            //再获取根节点里的所有子节点
            List<Element> elements = root.elements();
            System.out.println("elements:"+elements);
            //获取到OrderInfo子节点
            Iterator iter = root.elementIterator("OrderInfo");
            //遍历OrderInfo节点
            while(iter.hasNext()){
                Element recordEle = (Element) iter.next();
                String CustomerNo = recordEle.elementTextTrim("CustomerNo");
                String OrderNo = recordEle.elementTextTrim("OrderNo");
                String CodeUrl = recordEle.elementTextTrim("CodeUrl"); // 拿到CodeUrl节点下的子节点title值
//                System.out.println("CodeUrl:" + CodeUrl);
//                System.out.println("CustomerNo:" + CustomerNo);
//                System.out.println("OrderNo:" + OrderNo);
                map.put("CustomerNo",CustomerNo);
                map.put("OrderNo",OrderNo);
                map.put("CodeUrl",CodeUrl);

            }
            //System.out.println("map:"+map);


        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return map;
    }

    /**
     * 解析vbao 异步请求返回过来的报文信息
     * @Author yu
     * @date 2021/8/24 18:29
     * @Param [xml]
     * @return java.util.Map
     **/

    public static Map<String,String> parseNotifyUrlXml(String xml){
        Map<String,String> map = new HashMap<String,String>();
        //创建SAXReader对象
        SAXReader reader =  new SAXReader();
        //通过该对象读取输入的内容，使用文档对象接收’
        try {
            Document document = DocumentHelper.parseText(xml);
            //获取文档里的的根节点
            Element root =document.getRootElement();
            //再获取根节点里的所有子节点
            List<Element> elements = root.elements();
            System.out.println("elements:"+elements);
            //获取到OrderInfo子节点
            Iterator iter = root.elementIterator("OrderInfo");
            //遍历OrderInfo节点


            while(iter.hasNext()){
                Element recordEle = (Element) iter.next();

                String Sign = recordEle.elementTextTrim("Sign");
                String GoodNo = recordEle.elementTextTrim("GoodNo");
                String OrderNo = recordEle.elementTextTrim("OrderNo");
                String OrderAmount = recordEle.elementTextTrim("OrderAmount"); // 拿到CodeUrl节点下的子节点title值
                String SerialNo = recordEle.elementTextTrim("SerialNo");
                String PayTime = recordEle.elementTextTrim("PayTime");
                String PayStatus = recordEle.elementTextTrim("PayStatus");
                String ProductName = recordEle.elementTextTrim("ProductName");
                String Ext1 = recordEle.elementTextTrim("Ext1");

                map.put("Sign",Sign);
                map.put("GoodNo",GoodNo);
                map.put("OrderNo",OrderNo);
                map.put("OrderAmount",OrderAmount);
                map.put("SerialNo",SerialNo);
                map.put("PayTime",PayTime);
                map.put("PayStatus",PayStatus);
                map.put("ProductName",ProductName);
                map.put("Ext1",Ext1);
                map.put("PayStatus",PayStatus);

            }
            System.out.println("map:"+map);


        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return map;

    }



}
